//
//  THJAddContactView.m
//  THJXHW
//
//  Created by 河南德艺 on 17/3/30.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJAddContactView.h"
#import "SDAutoLayout.h"

@interface THJAddContactView()
{
    UIView * view;
}
@end

@implementation THJAddContactView

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}
-(void)setUI{
    
   
    view = [[UIView alloc] initWithFrame:CGRectMake(((ScreenWidth-(ScreenWidth/5*3+50))/2), ScreenHeight/4+30, ScreenWidth/5*3+50, ScreenHeight/2)];
    view.backgroundColor = SingleColor(whiteColor);
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = 8;
    [self addSubview:view];
    
    UILabel * titleLab = [[UILabel alloc] init];
    titleLab.text = @"添加联系人";
    titleLab.font = [UIFont fontWithName:@"Arial-BoldMT" size:25];
    titleLab.textColor = COLOR(33, 130, 249, 1);
    [view addSubview:titleLab];
    titleLab.sd_layout
    .leftSpaceToView(view,20)
    .topSpaceToView(view,10)
    .widthIs(ScreenWidth/5*3-20)
    .heightIs(30);
    
    
    UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancelBtn setBackgroundImage:kGetImage(@"叉") forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(dismissSelf) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:cancelBtn];
    cancelBtn.sd_layout
    .rightSpaceToView(view,10)
    .topSpaceToView(view,10)
    .widthIs(30)
    .heightIs(30);
    
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = COLOR(244, 244, 244, 1);
    [view addSubview:lineView];
    lineView.sd_layout
    .topSpaceToView(titleLab,10)
    .leftSpaceToView(view,0)
    .rightSpaceToView(view,0)
    .heightIs(1);
    
    
    UIView * tempLineView;
    UILabel * tempLab;
    NSArray * titleArr = @[@"姓名",@"电话",@"地址"];
    NSArray * tfPlaceholderArr = @[@"输入姓名(必填)",@"输入电话(必填)",@"输入公司地址(必填)"];
    for (NSInteger i = 0; i < titleArr.count; i++) {
        
        UILabel * nameLab = [[UILabel alloc] init];
        nameLab.text = titleArr[i];
        //nameLab.backgroundColor = SingleColor(brownColor);
        tempLab = nameLab;
        [view addSubview:nameLab];
        if (i == 0) {
            nameLab.sd_layout
            .leftSpaceToView(view,20)
            .topSpaceToView(lineView,10)
            .widthIs(35)
            .heightIs(30);
        }else{
            nameLab.sd_layout
            .leftSpaceToView(view,20)
            .topSpaceToView(tempLineView,10)
            .widthIs(35)
            .heightIs(30);
        }
        UITextField * nameTF = [[UITextField alloc] init];
        //nameTF.backgroundColor = COLOR(236, 239, 243, 1);
        nameTF.placeholder = tfPlaceholderArr[i];
        [view addSubview:nameTF];
        nameTF.tag = i+100;
        nameTF.sd_layout
        .topEqualToView(tempLab)
        .leftSpaceToView(tempLab,10)
        .rightSpaceToView(view,20)
        .heightIs(30);
        
        UIView * lineView1 = [[UIView alloc] init];
        lineView1.backgroundColor = COLOR(244, 244, 244, 1);
        [view addSubview:lineView1];
        tempLineView = lineView1;
        lineView1.sd_layout
        .leftSpaceToView(view,0)
        .topSpaceToView(tempLab,10)
        .rightSpaceToView(view,0)
        .heightIs(1);
    }
    
    UIButton * addBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [addBtn setTintColor:SingleColor(whiteColor)];
    [addBtn setTitle:@"添加联系人" forState:UIControlStateNormal];
    addBtn.backgroundColor = COLOR(251, 44, 8,1);
    [addBtn addTarget:self action:@selector(addContact) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:addBtn];
    addBtn.sd_layout
    .leftSpaceToView(view,0)
    .topSpaceToView(tempLineView,-1)
    .rightSpaceToView(view,0)
    .heightIs(40);
    [view setupAutoHeightWithBottomView:addBtn bottomMargin:0];
}
-(void)dismissSelf{
    [self removeFromSuperview];
}
-(void)addContact{
    [MBProgressHUD showMessage:@"正在提交" toView:self];
    UITextField * nametf = [view viewWithTag:100];
    UITextField * companyNameTf = [view viewWithTag:102];
    UITextField * phoneTf = [view viewWithTag:101];
//    UITextField * wechatTf = [view viewWithTag:103];
//    UITextField * weituoerTf = [view viewWithTag:104];
//    UITextField * addressTf = [view viewWithTag:105];
    if(nametf.text.length==0){
        [self HUDShowMsg:@"请输入姓名" AfterDelayDimiss:1];
        return;
    }else if(companyNameTf.text.length == 0){
        [self HUDShowMsg:@"请输入公司名" AfterDelayDimiss:1];
        return;
    }else if(phoneTf.text.length == 0){
        [self HUDShowMsg:@"请输入电话" AfterDelayDimiss:1];
        return;
    }
    
    NSString * urlstr = [NSString stringWithFormat:@"%@%@",THJDomain,THJStorePHP];
    NSString * mobile =[THJTools getMobile];
    NSDictionary * paraDic = @{
                               @"action":@"addlxuser",
                               @"mobile":mobile,
                               @"token":@"dev",
                               @"title":nametf.text,
                               @"client":@"",
                               @"dianhua":@"",
                               @"email":@"",
                               @"qqwei":@"",
                               @"note":@"",
                               @"company":@"",
                               @"addr":companyNameTf.text
                               };
    __weak typeof(self) weakself = self;
    [THJNetWork postNetDataFromUrl:urlstr parameters:paraDic callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"添加联系人--%@",receiveData);
        [MBProgressHUD hideHUDForView:weakself animated:YES];
    }];
}
-(void)HUDShowMsg:(NSString*)msg AfterDelayDimiss:(NSInteger)second{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = msg;
    //hud.label.backgroundColor = COLOR(0, 0, 0, 0.7);
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:second];
}

@end
